﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Reflection;

namespace Hack
{
    public class ExceptionInfo
    {
        public DateTime Date { get; private set; }
        public string Detail { get; private set; }
        public string FunctionName { get; private set; }
        public string Message { get; private set; }
        public string LineNumber { get; private set; }
        public string GOP { get; private set; }
        public string FileName { get; private set; }
        /// <summary>
        /// Can be null
        /// </summary>
        public MethodInfo MethodInfo { get; private set; }
        public Exception Exception { get; private set; }
        public ExceptionInfo(Exception ex)
        {
            this.Exception = ex;
            System.Diagnostics.StackTrace StackTrace = new System.Diagnostics.StackTrace(ex, true);
            System.Diagnostics.StackFrame StackFrame = StackTrace.GetFrame(0);
            string _FunctionName = StackFrame.GetMethod().Name;
            string _FileName = StackFrame.GetMethod().GetGenericArguments().ToString();
            string _te = ex.StackTrace.ToString();
            string gop = StackTrace.GetFrame(0).GetFileLineNumber().ToString();

            string _line;
            int _lis = _te.IndexOf("line");
            _line = _te.Substring(_lis);

            _lis = _te.IndexOf(" in ") + 4;
            int __lis =  _te.IndexOf(":line");
            FileName = _te.ToString().Substring(_lis,__lis - _lis);
            Date = DateTime.Now;
            Message = ex.Message;
            Detail = ex.StackTrace;
            FunctionName = _FunctionName;
            LineNumber = _line;
            GOP = gop;
            MethodInfo = null;
        }
        public ExceptionInfo(ExceptionInfo ex, MethodInfo methodInfo)
            : this(ex.Exception)
        {
            MethodInfo = methodInfo;
        }

        /// <summary>
        /// Returns a formated String of the ExceptionInfo
        /// </summary>
        /// <returns></returns>
        public string FormatedString
        {
            get
            {
                //return "Exception MethodName: " + this.MethodInfo.Name != null ? this.MethodInfo.Name : "Null" + "\n\r" +
                //       "Exception Date: " + DateTime.Now.ToString() + "\n\r" +
                //       "Exception Message : " + Message + "\n\r" +
                //       "Exception Function Name : " + FunctionName + "\n\r" +
                //       "Exception FileName : " + FileName + "\n\r" +
                //       "Exception On Line Number : " + LineNumber + "\n\r\n\r" +
                //       "Exception Detail : " + Detail + "\n\r";

                return String.Format("Exception MethodName: {1}{0}" +
                       "Exception Date: {2}{0}" +
                       "Exception Message: {3}{0}" +
                       "Exception Function Name: {4}{0}" +
                       "Exception FileName: {5}{0}" +
                       "Exception On Line Number: {6}{0}{0}" +
                       "Exception Detail: {7}{0}",
                       Environment.NewLine,

                       this.MethodInfo.Name != null ? this.MethodInfo.Name : "Null",
                        DateTime.Now.ToString(),
                        Message,
                        FunctionName,
                        FileName,
                        LineNumber,
                        Detail);


            }
        }
    }
}
